/*
 * @lc app=leetcode.cn id=3 lang=cpp
 *
 * [3] 无重复字符的最长子串
 */
using namespace std;
#include <string>
#include <set>

// @lc code=start
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int max = 0;
        
        set<char> chars;
        int i = 0;
        int j = 0;

        for (j = 0; j < s.length(); ){
            if(!chars.count(s[j])){
                chars.insert(s[j]);
                j++;
                if (j - i > max){
                    max = j - i;
                }
            }
            else{
                chars.erase(s[i]);
                i++;
            }
        }

        return max;
    }
};
// @lc code=end

